<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel='stylesheet' href='../rurple.css' type='text/css'>
<title>28. Variables and functions.</title>
</head>
<body>
<h2 class="title">28. Variables and functions</h2>

<p>Variables, which we have seen in the previous lesson, are especially useful in function definitions. Remember our <tt>print2</tt> function?</p>
<pre>
&gt;&gt;&gt; <span class="keyword">def</span> print2():
...     <span class="keyword">print</span> 2
...    
&gt;&gt;&gt; print2()
2
</pre>
<p>We can make it more general as follows:</p>
<pre>
&gt;&gt;&gt; <span class="keyword">def</span> print_number(n):
...     <span class="keyword">print</span> n
...    
&gt;&gt;&gt; print_number(2)
2
&gt;&gt;&gt; print_number(78)
78
</pre>
<p>By writing a variable as an <i>argument</i> between the two parentheses when we define the function, we tell Python that, whatever value we use when we <i>call</i> the function must be assigned to the variable (<tt>n</tt> in this case).</p>
<!--==============================-->
<hr width="50%">
<h3 class="section">Combining numbers and strings</h3>
<p>Suppose we want to print both numbers and strings together. By this, I
mean, suppose we want the output of the <tt>print_number()</tt> function to
be something like:</p>
<pre>
The number you have chosen is 2.
</pre>
<p>There are <b><u>many</u></b> ways to do this as you will see later. Here's
one way which is not obvious, but which is very useful to know.</p>
<pre>
&gt;&gt;&gt; <span class="keyword">def</span> print_number(n):
....    <span class="keyword">print</span> <span class=
"string">"The number you have chosen is %s."</span> % n
....    
&gt;&gt;&gt; print_number(3)
The number you have chosen is 3.
</pre>
<p>Python replaces the combination <tt>%s</tt> inside the string by the value
of the variable that follows the <tt>%</tt> after the string. Try it on your
own, and then keep on reading.</p>
<!--==============================-->
<hr width="50%">
<h3 class="section">Moving on to greater things</h3>
<p>Let's go back to our <tt>greater()</tt> function and make it more general.
This is how we can do it</p>
<pre>
&gt;&gt;&gt; <span class="keyword">def</span> greater(first, second):
...     <span class="keyword">if</span> first &gt; second:
...         <span class="keyword">print</span> <span class=
"string">"%s is greater than %s"</span> % (first, second)
...     <span class="keyword">else</span>:
...         <span class="keyword">print</span> <span class=
"string">"%s is greater than %s"</span> % (second, first)
...     
&gt;&gt;&gt; greater(5, 4)
5 is greater than 4
&gt;&gt;&gt; greater(0, 10)
10 is greater than 0
</pre>
<p>Note that we must put parentheses <tt>()</tt> around the two variable that
follow the percent <tt>%</tt> sign so that Python knows that we mean to use
them to replace the two <tt>%s</tt> that are inside the string.</p>
<p><b>Try it!</b></p>
<!--==============================-->
<hr width="50%">
<h3 class="section">Moving on to even greater things</h3>
<p>We have just seen how we could make our function <tt>greater()</tt> more
general than always comparing the same two values (as it was before) by using
variables. Now, as it is written, it always require two <i>arguments</i>: the
two numbers to compare. Suppose we would like have Python decide if some
numbers are greater than the special value 0 (zero). We could write another
function to do that ... or we could change slightly our existing function as
follows:</p>
<pre>
&gt;&gt;&gt; <span class="keyword">def</span> greater(first, second=0):
...     <span class="keyword">if</span> first &gt; second:
...         <span class="keyword">print</span> <span class=
"string">"%s is greater than %s"</span> % (first, second)
...     <span class="keyword">else</span>:
...         <span class="keyword">print</span> <span class=
"string">"%s is greater than %s"</span> % (second, first)
...     
&gt;&gt;&gt; greater(5, 4)
5 is greater than 4
&gt;&gt;&gt; greater(1, 10)
10 is greater than 1
greater(5)
&gt;&gt;&gt; 5 is greater than 0
greater(-2)
&gt;&gt;&gt; 0 is greater than -2
</pre>
<p>By writing "<tt>second=0</tt>" between the parentheses in the function
definition, we are telling Python to use that value for the second variable
if we call the function with only one argument. When we give such a default
value to an argument, we call it a named argument.</p>
<p>Named arguments (there can be more than one) <b><u>must</u></b> always
appear <b><u>after</u></b> the unnamed arguments. Therefore, the following
definitions [only the first line is shown below] would be allowed:</p>
<pre>
&gt;&gt;&gt; <span class="keyword">def</span> four_arguments(a, b, c, d=33):
&gt;&gt;&gt; <span class="keyword">def</span> four_arguments(a, b, c=7, d=2):
&gt;&gt;&gt; <span class="keyword">def</span> four_arguments(a, b=1, c=3, d=56):
&gt;&gt;&gt; <span class="keyword">def</span> four_arguments(a=4, b=1, c=3, d=0):
</pre>
<p>but the following definitions would <b><u>not</u></b> be allowed:</p>
<pre>
&gt;&gt;&gt; <span class="keyword">def</span> four_arguments(a=1, b, c, d=33):
&gt;&gt;&gt; <span class="keyword">def</span> four_arguments(a, b=2, c=3, d):
&gt;&gt;&gt; <span class="keyword">def</span> four_arguments(a=5, b=1, c=3, d):
</pre>
<p>Try it while making up your own function definitions.</p>
<!--==============================-->
<hr width="50%">
<h3 class="section">Final word of caution</h3>
<p>Python does not allow you to use one of its keyword as a variable.  Try the following:</p>
<pre>
&gt;&gt;&gt; <span class="keyword">def</span> = 2
</pre>
<p>and see what you get!</p>

<center><a href="27-variables.htm"><img alt="previous" src=
"../../images/previous.png">Variables.</a> - <a href=
"../lessons_toc.htm"><img alt="home" src="../../images/home.png"></a> - <a href=
"29-strings.htm">What's your name?<img alt="next"
src="../../images/next.png"></a></center>
</body>
</html>
